{extend name="../layout/base" /}

{block name="tool_bar"}
<form class="layui-form" action="">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-xs2">
            <div class="layui-form-item">
                <select name="type2" lay-filter="types">
                    <option {if $app_type=='all'} selected {/if} value="all">所有</option>
                    <option {if $app_type=='mp'} selected {/if} value="mp">公众号</option>
                    <option {if $app_type=='mini'} selected {/if} value="mini">小程序</option>
                </select>
            </div>
        </div>
        <div class="layui-col-xs7">
            <div class="layui-col-xs5" style="margin-left: 5px; line-height: 38px;">
                <div class="layui-form-item">
                    <input name="search_key"
                           placeholder="请输入关键词"
                           value="{$search_key}"
                           class="layui-input"
                           type="text">
                </div>
            </div>
            <div class="layui-col-xs4" style="margin-left: 5px; line-height: 38px;">
                <button class="layui-btn layui-btn-primary" lay-submit="" lay-filter="formDemo">
                    <i class="fa fa-search"></i>
                </button>
            </div>
        </div>
    </div>
</form>
{/block}

{block name="body"}
<div>
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li {eq name="type" value="uninstall"}class="layui-this"{/eq}> <a href="{:url('index',['type'=>'uninstall'])}">已上架</a></li>
            <li {eq name="type" value="onsale"}class="layui-this"{/eq}> <a href="{:url('index',['type'=>'onsale'])}">下架中</a></li>
            <li {eq name="type" value="notinstall"}class="layui-this"{/eq}><a href="{:url('index',['type'=>'notinstall'])}">未安装</a></li>
        </ul>
    </div>

    <table class="layui-table" lay-even lay-skin="nob">
        <thead>
        <tr>
            <th width="80">应用Logo</th>
            <th width="80">应用名称</th>
            <th>应用标识</th>
            {if $type == 'uninstall'}
            <th>年费</th>
            <th>销量</th>
            {/if}
            <th>版本</th>
            <th>作者</th>
            {if $type != 'notinstall'}
            <th>templateID</th>
            {/if}
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {volist name="addons" id="vo"}
        <tr>
            <td>
                <img width="30" height="30" src="{$vo.logo}">
            </td>
            <td>{$vo.name}</td>
            <td>{$vo.addon}</td>
            {if $type == 'uninstall'}
            <td>¥{$vo.price}</td>
            <td>{$vo.sale_num}</td>
            {/if}
            <td>{$vo.version}</td>
            <td>{$vo.author}</td>
            {if $type != 'notinstall'}
            <td>{$vo.template_id ?? ''}</td>
            {/if}
            <td>
                {switch name="type"}
                {case value="uninstall"}
                <a class="layui-btn layui-btn-xs layui-btn-normal" href="{:url('edit',['id'=>$vo.id])}">编辑</a>
                <button class="layui-btn layui-btn-xs" onclick="closeApp('{$vo.addon}')">下架</button>
                {/case}
                {case value="onsale"}
                <a class="layui-btn layui-btn-xs layui-btn-normal" href="{:url('edit',['id'=>$vo.id])}">编辑</a>
                {if $vo.template_id || $vo.type=='mp'}
                <button class="layui-btn layui-btn-xs " onclick="installApp('{$vo.addon}')">上架</button>
                {/if}
                <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="wipeDataApp('{$vo.addon}')">还原</button>
                {/case}
                {case value="notinstall"}
                <button class="layui-btn layui-btn-xs " onclick="installApp('{$vo.addon}')">安装</button>
                <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="uninstallApp('{$vo.addon}')">卸载</button>
                {/case}
                {/switch}
                {if in_array('mini', $vo.type) and $type != 'notinstall'}
                <a class="layui-btn layui-btn-xs layui-btn-warm" href="{:url('config',['id'=>$vo.id])}">配置</a>
                <a class="layui-btn layui-btn-xs layui-btn-warm" href="{:url('setTemplate',['id'=>$vo.id])}">设置模版</a>
                {/if}
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>

    <div id="page">{$page|raw}</div>
</div>
{/block}

{block name="js"}
<script>
    var layer;

    layui.use(['layer', 'form'], function(){
        layer = layui.layer;
        var form = layui.form;
        form.on('select(types)', function(data){
            window.location.href="/admin/app/index/type/{$type}/app_type/"+data.value+"/search_key/{$search_key}";
        });
    });

    /**
     * 安装/上架
     */
    function installApp(name) {
        let loadIndex = layer.load(1); //加载图标
        $.post("{:url('admin/app/install')}", {'name':name}, function (res) {
            layer.close(loadIndex);
            if(res.code === 1){
                layer.msg(res.msg, {time: 1500}, function () {
                    location.href = res.url;
                });
            }else{
                layer.alert(res.msg, {title: '错误提示'}, function () {
                    if(res.url){
                        location.href = res.url;
                    }
                });
            }
        }).error(function (error) {
            layer.alert('500 Internal Server Error', {title: '错误提示'});
            layer.close(loadIndex)
        });
        return false;
    }

    /**
     * 升级
     * @param name
     */
    function upgrade(name) {
        $.post("{:url('admin/app/upgrade')}",{'name':name},function (res) {
            if(res.code === 1){
                layer.msg(res.msg, {time:1000}, function () {
                    location.href = "{:url('admin/app/index')}";
                });
            }else{
                layer.alert(res.msg);
            }
        })
    }

    /**
     * 下架
     * @param name
     */
    function closeApp(name) {
        $.post("{:url('admin/app/close')}", {'name':name}, function (res) {
            if(res.code === 1){
                layer.msg(res.msg, {time:1000}, function () {
                    location.href="{:url('admin/app/index', ['type'=>'uninstall'])}";
                });
            }else{
                layer.alert(res.msg);
            }
        })
    }

    /**
     * 卸载
     * @param name
     */
    function uninstallApp(name) {
        layer.confirm('你确定卸载（'+name+'）应用吗？点击确定将会清空此应用所有数据以及文件', {
            btn: ['确定','不的']
        }, function(load_index){
            layer.close(load_index);
            var index = layer.load(1,{
                shade: [0.1,'#000']
            });
            $.post("{:url('admin/app/uninstall')}", {'name':name}, function (res) {
                if(res.code === 1){
                    layer.msg(res.msg, {time:1000}, function () {
                        location.href="{:url('admin/app/index',['type' => 'notinstall'])}";
                    });
                }else{
                    layer.close(index);
                    layer.alert(res.msg);
                }
            }).error(function (error) {
                layer.alert('500 Internal Server Error');
                layer.close(index)
            })
        }, function(){});
    }

    /**
     * 还原
     * @param name
     */
    function wipeDataApp(name) {
        layer.confirm('你确定还原（'+name+'）应用吗？点击确定将会清空数据以及还原未安装状态', {
            btn: ['确定','不的']
        }, function(load_index){
            layer.close(load_index);
            let index = layer.load(1);
            $.post("{:url('admin/app/wipeData')}", {'name':name}, function (res) {
                layer.close(index);
                if(res.code === 1){
                    layer.msg(res.msg, {time:1000}, function () {
                        location.href="{:url('admin/app/index',['type'=>'uninstall'])}";
                    });
                }else{
                    layer.alert(res.msg, {title: '错误提示'});
                }
            }).error(function (error) {
                layer.alert('500 Internal Server Error');
                layer.close(index)
            })
        }, function(){});
    }
</script>
{/block}